/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package actionsUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.beans.User;
import models.beans.UserBean;
import models.dao.UserDAO;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Group 3 Aprotrain Aptech Group3 Aprotrain Aptech
 */
public class changePassStudent extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        User user = (User) form;
        User userAtt = (User) request.getSession().getAttribute("user");
      
        UserBean userBean = new UserBean();
        userBean.setUserId(userAtt.getUserId());
        userBean.setUserName(userAtt.getUserName());
        userBean.setSex(userAtt.getSex());
        userBean.setIsStaff(0);
        userBean.setAddress(user.getAddress());
        userBean.setBirthday(user.getBirthday());
        userBean.setClassUser(userAtt.getClassUser());
        userBean.setSex(userAtt.getSex());
        if(user.getOldPassword().length()>0 && user.getNewPassword().length()>0 && user.getReNewPassword().length()>0){
            userBean.setPassword(user.getNewPassword());          
        }
        else{
            userBean.setPassword(userAtt.getPassword());
        }
        userBean.setIsFirstLogin(1);
        UserDAO.updateUser(userBean);        
        request.getSession().removeAttribute("changePassStudent");
        request.getSession().removeAttribute("changePassStaff");        
        return mapping.findForward(SUCCESS);
    }
}
